/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.easemob.chatui.db;

import android.content.Context;
import android.text.TextUtils;
import com.easemob.chatui.Constant;
import com.easemob.chatui.domain.User;
import com.easemob.util.HanziToPinyin;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBase;
import com.lyw.GeoLocationClient.App;
import timber.log.Timber;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserDao {
	public static final String TABLE_NAME = "uers";
	public static final String COLUMN_NAME_ID = "username";
	public static final String COLUMN_NAME_NICK = "nick";
	public static final String COLUMN_NAME_AVATAR = "avatar";
    public static final String USER_DATA_NAME = "users.db";
    private static UserDao instance;
	private DataBase dbHelper;

    public static UserDao getInstance(Context context) {
        synchronized (UserDao.class) {
            if (instance == null) {
                instance = new UserDao(context);
            }
        }
        return instance;
    }

	public UserDao(Context context) {
        dbHelper = LiteOrm.newSingleInstance(context, USER_DATA_NAME);
    }

	/**
	 * 保存好友list
	 * 
	 * @param contactList
	 */
	public void saveContactList(List<User> contactList) {
        Timber.e("save contact list " );
        dbHelper.save(contactList);
    }

	/**
	 * 获取好友list
	 * 
	 * @return
	 */
	public Map<String, User> getContactList() {
        ArrayList<User> userArrayList = dbHelper.query(User.class);
        Map<String, User> users = new HashMap<String, User>();
        for (int i = 0; i < userArrayList.size(); i++) {
            User user = userArrayList.get(i);
            String username = user.getUsername();
            String headerName = null;
            if (!TextUtils.isEmpty(user.getNick())) {
                headerName = user.getNick();
            } else {
                headerName = user.getUsername();
            }

            if (username.equals(Constant.NEW_FRIENDS_USERNAME) || username.equals(Constant.GROUP_USERNAME)) {
                user.setHeader("");
            } else if (Character.isDigit(headerName.charAt(0))) {
                user.setHeader("#");
            } else {
                user.setHeader(HanziToPinyin.getInstance().get(headerName.substring(0, 1))
                        .get(0).target.substring(0, 1).toUpperCase());
                char header = user.getHeader().toLowerCase().charAt(0);
                if (header < 'a' || header > 'z') {
                    user.setHeader("#");
                }
            }
            users.put(username, user);
        }

        return users;
    }

	public void deleteContact(String username){
        User user = App.getInstance().getContactList().get(username);
        deleteContact(user);
	}

    public void deleteContact(int id) {
        dbHelper.delete(id);
    }

    public void deleteContact(User user) {
        dbHelper.delete(user);
    }

    /**
	 * 保存一个联系人
	 * @param user
	 */
	public void saveContact(User user) {
        dbHelper.save(user);
    }

    public void updateContact(User user) {
        dbHelper.update(user);
    }
}
